import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import VideoPlayView from "@/views/VideoPlayView.vue";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: HomeView
        },
        {
            path: '/video-play',
            name: 'video-play',
            component: VideoPlayView
        },
        {
            path: '/about',
            name: 'about',
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import('../views/AboutView.vue'),
            // 定义meta数据
            meta: {
                // 是否需要登录访问
                requiresAuth: true
            }
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/LoginView.vue')
        },
        {
            path: '/404',
            name: '404',
            component: () => import('../views/404.vue'),
            beforeEnter: (to, from, next) => {
                console.log('Router Guard')
                console.log(to, from)

                next()
            }
        },
        {
            path: '/:catchAll(.*)*',
            // 上面的都匹配不到，就重定向到404
            redirect: {name: '404'}
        }
    ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
    console.log('Global Guard')
    console.log(to, from)
    // window.axiosPromiseArr.forEach((ele,index) => {
    //     ele.cancel() // 路由跳转之前，终止上一个页面正在请求的内容
    //     // 清空请求的参数
    //     delete window.axiosPromiseArr[index]
    // });

    next()
})

export default router
